package com.sys.console.action;


import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.hyperic.sigar.Sigar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sys.console.business.SysEmailConfigService;
import com.sys.console.business.SysMonitorService;
import com.sys.console.common.SystemInfo;
import com.sys.console.pojo.SysEmailConfig;
import com.sys.console.pojo.SysMonitor;
import com.sys.object.common.ObjException;
import com.sys.object.common.ObjResultCode;
import com.sys.object.common.ObjResultJSON;
import com.sys.object.common.email.MailSendUtils;



/**
 * 系统监控Controller
 * @author zxp
 * @version 2016-02-07
 */
@Controller
public class MonitorController {
	@Autowired
	private SysMonitorService monitorService;
	@Autowired SysEmailConfigService sysEmailConfigService;
	
	@ModelAttribute
	public SysMonitor get(@RequestParam(required=false) String id,HttpServletRequest request) {
		SysMonitor entity = null;
		try{
			if (StringUtils.hasText(id)){
				entity = monitorService.selectByPrimaryKey(id,request);
			}
			if (entity == null){
				entity = new SysMonitor();
			}
			return entity;
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping("/info.do")
	public String info(Model model,HttpServletRequest request) throws Exception {
		SysMonitor sysMonitor = monitorService.selectByPrimaryKey("1",request);
		model.addAttribute("sysMonitor", sysMonitor);
		return  "/system/monitor/info";
	}
	
	@RequestMapping("/monitor.do")
	public String monitor() throws Exception {
		return "/system/monitor/monitor";
	}
	
	@RequestMapping("/systemInfo.do")
	public String systemInfo(Model model) throws Exception {
		model.addAttribute("systemInfo", SystemInfo.SystemProperty());
		return "/system/monitor/systemInfo";
	}
	
	@ResponseBody
	@RequestMapping("/usage.do")
	public Map usage(Model model,HttpServletRequest request) throws Exception {
		SysEmailConfig config = sysEmailConfigService.selectByPrimaryKey("1",request);
		SysMonitor monitor = monitorService.selectByPrimaryKey("1",request);
		Map<?, ?> sigar = SystemInfo.usage(new Sigar());
		String content="";
		content += "您预设的cpu使用率警告线是"+monitor.getCpu()+"%, 当前使用率是"+sigar.get("cpuUsage")+"%";
		content += "您预设的jvm使用率警告线是"+monitor.getJvm()+"%, 当前使用率是"+sigar.get("jvmUsage")+"%";
		content += "您预设的ram使用率警告线是"+monitor.getRam()+"%, 当前使用率是"+sigar.get("ramUsage")+"%";
		if(Float.valueOf(sigar.get("cpuUsage").toString()) >= Float.valueOf(monitor.getCpu())
				||Float.valueOf(sigar.get("jvmUsage").toString()) >= Float.valueOf(monitor.getJvm())
				||Float.valueOf(sigar.get("ramUsage").toString()) >= Float.valueOf(monitor.getRam())){
			MailSendUtils.sendEmail(config.getSmtp(), config.getPort(), config.getMailName(), config.getMailPassword(), monitor.getToEmail(), "服务器监控预警", content, "0");
			
		};
		return sigar;
	}
	/**
	 * 修改配置　
	 * @param request
	 * @param nodeId
	 * @return
	 * @throws Exception
	 */
    @ResponseBody
	@RequestMapping("/modifySetting.do")
    public ObjResultJSON<SysMonitor> modifySetting(SysMonitor sysMonitor, Model model,HttpServletRequest request) {
    	ObjResultJSON<SysMonitor> ret = new ObjResultJSON<SysMonitor>();
		try {
			//SysMonitor t = monitorService.selectByPrimaryKey("1",request);
			sysMonitor.setId("1");
			//MyBeanUtils.copyBeanNotNull2Bean(monitor, t);
			int suc = monitorService.saveOrUpdate(sysMonitor,request);
			if(suc>0){
				ret.setResultCode(ObjResultCode.OK);
				ret.setData(sysMonitor);
			}
		} catch (Exception e) {
			e.printStackTrace();
			if (e.getClass().equals(ObjException.class)) {
				ret.setResultCode(((ObjException) e).getCode());
			} else {
				ret.setResultCode(ObjResultCode.ERROR);
			}
		}
		return ret;
    }
}